<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="字典名称">
              <a-input placeholder="请输入字典名称" v-model="queryParam.name"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="字典代码">
              <a-input placeholder="请输入字典代码" v-model="queryParam.type"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-button @click="() => {this.queryPage()}" type="primary">查询</a-button>
            <a-button @click="() => queryParam = {}" style="margin-left: 8px">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <v-table
      :columns="columns"
      :data="loadData"
      bordered
      ref="table"
    >
      <template v-slot:buttons>
        <a-button type="primary" icon="plus" @click="$refs.dictAddOrUpdate.init('','add')" v-action="'sys:dict:save'">新建</a-button>
      </template>
      <span slot="action" slot-scope="{row,text}">
        <span v-action="'sys:dict:update'">
          <a @click="edit(row)">修改</a>
          <a-divider type="vertical"/>
        </span>
        <span>
          <a @click="showDictItem(row)" href="javascript:">字典配置</a>
          <a-divider type="vertical"/>
        </span>
        <span>
          <a @click="showLabel(row)" href="javascript:">标签配置</a>
          <a-divider type="vertical"/>
        </span>
        <span v-action="'sys:dict:del'">
          <a-popconfirm @confirm="del(row)" cancelText="否" okText="是" title="是否删除字典">
            <a-icon slot="icon" style="color: red" type="question-circle-o"/>
            <a href="javascript:" style="color: red">删除</a>
          </a-popconfirm>
        </span>
      </span>
    </v-table>
    <dict-add-or-update
      @ok="queryPage"
      ref="dictAddOrUpdate"
    />
    <dict-item-drawer
      ref="dictItemDrawer"
    />
    <label-item-drawer
      ref="labelItemDrawer"
    />
  </a-card>
</template>

<script>
import { delObj, fetchList } from '@api/sys/dict'
import DictAddOrUpdate from './dictAddOrUpdate'
import DictItemDrawer from './dictItemDrawer'
import LabelItemDrawer from '../label/dictLabelDrawer'
import { TableMixin } from '@/mixins/TableMixin'

export default {
  name: 'Dict',
  mixins: [TableMixin],
  components: {
    DictAddOrUpdate,
    DictItemDrawer,
    LabelItemDrawer
  },
  data () {
    return {
      columns: [
        {
          title: '字典名称',
          width: '10%',
          align: 'center',
          field: 'name'
        },
        {
          title: '字典类型',
          width: '10%',
          align: 'center',
          field: 'type'
        },
        {
          title: '描述',
          field: 'remark'
        },
        {
          title: '操作',
          width: '400px',
          field: 'action',
          slot: true
        }
      ],
      queryParam: {
        name: '',
        type: ''
      },
      loadData: parameter => {
        return fetchList(
          Object.assign(parameter, this.queryParam)
        ).then(res => {
          return res.data
        })
      }
    }
  },
  methods: {
    edit (record) {
      this.$refs.dictAddOrUpdate.init(record.id, 'edit')
    },
    del (record) {
      delObj(record.id).then(_ => {
        this.$message.info('删除成功')
        this.queryPage()
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    showDictItem (record) {
      this.$refs.dictItemDrawer.init(record)
    },
    showLabel (record) {
      this.$refs.labelItemDrawer.init(record)
    }
  }
}
</script>

<style scoped>

</style>
